package cn.elead.chaos.sso.server.utli;

import cn.elead.chaos.framework.thread.ThreadContext;
import cn.elead.chaos.sso.server.web.system.domain.SysUser;
import cn.hutool.core.util.StrUtil;

/**
 * 当前用户
 * 
 * @author luopeng
 *
 */
public class Current {

	public static final String CURRENT_USER_KEY = Current.class.getName() + "_CURRENT_USER_KEY";

	public static final String CACHE_USER = "cache:current:user:";

	public static SysUser getUser() {
		return (SysUser) ThreadContext.get(CURRENT_USER_KEY);
	}

	public static String getUserId() {
		SysUser user = getUser();
		if (null != user) {
			return user.getUserId();
		}
		return null;
	}

	public static String getUserName() {
		SysUser user = getUser();
		if (null != user) {
			return user.getUserName();
		}
		return null;
	}
	
	public static boolean isAdmin() {
		SysUser user = getUser();
		return isAdmin(user);
	}
	
	
	public static boolean isAdmin(SysUser user) {
		if (null != user && StrUtil.equals(user.getAdmin(), "1")) {
			return true;
		}
		return false;
	}

	public static void bind(SysUser user) {
		if (user != null) {
			ThreadContext.put(CURRENT_USER_KEY, user);
		}
	}

	public static SysUser unbindUser() {
		return (SysUser) ThreadContext.remove(CURRENT_USER_KEY);
	}
}
